﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#if DOT_NET
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
#endif
using StyleMVVM.DependencyInjection;
using StyleMVVM.UnitTests.TestClasses.SimpleObjects;

namespace StyleMVVM.UnitTests.DependencyInjection
{
	[TestClass]
	public class SortOrderTests
	{
		[TestMethod]
		public void RunTimeSort()
		{
			Bootstrapper bootstrapper = new Bootstrapper();

			bootstrapper.Container.Environment = ExportEnvironment.RunTime;

			bootstrapper.Container.Register<UnitTestSortTestExportInterface>().
			             As<ISortTestExportInterface>().InEnvironment(ExportEnvironment.UnitTest);

			bootstrapper.Container.Register<DesignTimeSortTestExportInterface>().
			             As<ISortTestExportInterface>().InEnvironment(ExportEnvironment.DesignTime);

			bootstrapper.Container.Register<RunTimeSortTestExportInterface>().
			             As<ISortTestExportInterface>().InEnvironment(ExportEnvironment.RunTime);

			bootstrapper.Container.Register<AnySortTestExportInterface>().
			             As<ISortTestExportInterface>().InEnvironment(ExportEnvironment.Any);

			bootstrapper.Start();

			List<ISortTestExportInterface> list = new List<ISortTestExportInterface>(
				bootstrapper.Container.LocateAll<ISortTestExportInterface>());

			Assert.IsInstanceOfType(list[0], typeof(RunTimeSortTestExportInterface));

			Assert.IsInstanceOfType(list[1], typeof(AnySortTestExportInterface));

			bootstrapper.Shutdown();
		}

		[TestMethod]
		public void DesignTimeSort()
		{
			Bootstrapper bootstrapper = new Bootstrapper();

			bootstrapper.Container.Environment = ExportEnvironment.DesignTime;

			bootstrapper.Container.Register<UnitTestSortTestExportInterface>().
			             As<ISortTestExportInterface>().InEnvironment(ExportEnvironment.UnitTest);

			bootstrapper.Container.Register<DesignTimeSortTestExportInterface>().
			             As<ISortTestExportInterface>().InEnvironment(ExportEnvironment.DesignTime);

			bootstrapper.Container.Register<RunTimeSortTestExportInterface>().
			             As<ISortTestExportInterface>().InEnvironment(ExportEnvironment.RunTime);

			bootstrapper.Container.Register<AnySortTestExportInterface>().
			             As<ISortTestExportInterface>().InEnvironment(ExportEnvironment.Any);

			bootstrapper.Start();

			int i = 0;

			List<ISortTestExportInterface> list = new List<ISortTestExportInterface>(
				bootstrapper.Container.LocateAll<ISortTestExportInterface>());

			Assert.IsInstanceOfType(list[0], typeof(DesignTimeSortTestExportInterface));

			Assert.IsInstanceOfType(list[1], typeof(AnySortTestExportInterface));

			bootstrapper.Shutdown();
		}

		[TestMethod]
		public void UnitTestSort()
		{
			Bootstrapper bootstrapper = new Bootstrapper();

			bootstrapper.Container.Environment = ExportEnvironment.UnitTest;

			bootstrapper.Container.Register<UnitTestSortTestExportInterface>().
			             As<ISortTestExportInterface>().InEnvironment(ExportEnvironment.UnitTest);

			bootstrapper.Container.Register<DesignTimeSortTestExportInterface>().
			             As<ISortTestExportInterface>().InEnvironment(ExportEnvironment.DesignTime);

			bootstrapper.Container.Register<RunTimeSortTestExportInterface>().
			             As<ISortTestExportInterface>().InEnvironment(ExportEnvironment.RunTime);

			bootstrapper.Container.Register<AnySortTestExportInterface>().
			             As<ISortTestExportInterface>().InEnvironment(ExportEnvironment.Any);

			bootstrapper.Start();

			int i = 0;

			List<ISortTestExportInterface> list = new List<ISortTestExportInterface>(
				bootstrapper.Container.LocateAll<ISortTestExportInterface>());

			Assert.IsInstanceOfType(list[0], typeof(UnitTestSortTestExportInterface));

			Assert.IsInstanceOfType(list[1], typeof(AnySortTestExportInterface));

			bootstrapper.Shutdown();
		}

		[TestMethod]
		public void PriorityTest()
		{
			Bootstrapper bootstrapper = new Bootstrapper();

			bootstrapper.Container.Environment = ExportEnvironment.UnitTest;

			bootstrapper.Container.RegisterExportFunction(
				(x, y, z) => new RunTimeSortTestExportInterface()).
			             As<ISortTestExportInterface>().
			             InEnvironment(ExportEnvironment.UnitTest).
			             WithPriority(5).
			             InitializeProperty(x => x.TestField, 5);

			bootstrapper.Container.RegisterExportFunction(
				(x, y, z) => new RunTimeSortTestExportInterface()).
			             As<ISortTestExportInterface>().
			             InEnvironment(ExportEnvironment.UnitTest).
			             WithPriority(10).
			             InitializeProperty(x => x.TestField, 10);

			bootstrapper.Start();

			int i = 0;

			List<ISortTestExportInterface> list = new List<ISortTestExportInterface>(
				bootstrapper.Container.LocateAll<ISortTestExportInterface>());

			Assert.AreEqual(10, list[0].TestField);
			Assert.AreEqual(5, list[1].TestField);

			bootstrapper.Shutdown();
		}
	}
}